#include <map>
#include <vector>
#include <string>
#include "smartpipe.h"

using namespace std;
using namespace sp;

void registe(){
    Image::Crop::cropWithInput::registe();
    Image::Crop::cropWithParas::registe();
    Image::Gen::genFromDisk::registe();
    Image::Gen::genFromMemory::registe();
    Image::Resize::letterBoxResize::registe();
    Image::Resize::resize::registe();
    Image::Save::markAndSave::registe();
    Image::Save::save::registe();
    Image::Trans::trans::registe();
    Model::Group::groupByBatch::registe();
    Model::Split::splitByShape::registe();
    Model::Trans::transferToDeviceMemory::registe();
    Model::Trans::transferToHostMemory::registe();
    Model::Yolo::yolo_complete::registe();
    Model::Yolo::yolo_preprocess::registe();
    Model::Yolo::yolo_inference::registe();
    Model::Yolo::yolo_postprocess::registe();
    Model::Retinanet::retinanet_preprocess::registe();
    Model::Retinanet::retinanet_inference::registe();
    Model::Retinanet::retinanet_postprocess::registe();
    Model::LPRnet::lprnet_preprocess::registe();
    Model::LPRnet::lprnet_inference::registe();
    Model::LPRnet::lprnet_postprocess::registe();
    Model::Openpose::openpose_preprocess::registe();
    Model::Openpose::openpose_inference::registe();
    Model::Openpose::openpose_postprocess::registe();
    Tool::Group::groupByRequestId::registe();
    Tool::Split::splitByFlowId::registe();
}

char* DATA_SOURCE;
char* DATA_TARGET;

char* input_video_data_ptr;
char* output_video_data_ptr;

// 共用参数
string video_path = "/data/lx/SmartPipe/data_source/videos/0.mp4";                      // 视频路径
string save_path = "/data/lx/SmartPipe/apps/car_license_plate_recognition/output.avi";  // 输出路径
int video_channels = 1;               // 视频路数
long single_cnt = 1000;               // 单路视频帧数
int single_fps = 1000;                // 单路视频帧率
long cnt = single_cnt*video_channels; // 处理帧数

void test(SharedMemoryManager& smm, Gpu_SharedMemoryManager& gpu_smm){
    // 构造app
    Function* f0 = new Image::Gen::genFromMemory(input_video_data_ptr, cnt, single_fps*video_channels, 3840, 2160); // 1
    Function* f1 = new Image::Resize::resize(640, 384);             // 4
    Function* f2 = new Model::Yolo::yolo_preprocess(640, 384);      // 1
    Function* f3 = new Model::Group::groupByBatch(8);               // 1
    Function* f4 = new Model::Trans::transferToDeviceMemory();      // 1
    Function* f5 = new Model::Yolo::yolo_inference(640, 384);       // 1
    Function* f6 = new Model::Trans::transferToHostMemory();        // 1
    Function* f7 = new Model::Split::splitByShape();                // 1
    Function* f8 = new Model::Yolo::yolo_postprocess(640, 384);     // 1
    // 连接逻辑关系
    connectOneByOne(9, f0, f1, f2, f3, f4, f5, f6, f7, f8);
    // 扩展多instance
    Function* f1_0;
    Function* f1_1;
    Function* f1_2;
    // Function* f1_3;
    // Function* f1_4;
    // Function* f2_0;
    // Function* f2_1;
    // Function* f2_2;
    // Function* f3_0;
    // Function* f3_1;
    // Function* f3_2;
    // Function* f6_0;
    // Function* f7_0;
    // Function* f7_1;
    // Function* f7_2;
    // Function* f8_0;
    // Function* f8_1;
    // Function* f8_2;
    expand(f1, 3, &f1_0, &f1_1, &f1_2);
    // expand(f2, 1, &f2_0);
    // expand(f3, 1, &f3_0);
    // expand(f6, 1, &f6_0);
    // expand(f7, 1, &f7_0);
    // expand(f8, 3, &f8_0, &f8_1, &f8_2);
    // 部署表
    map<short, vector<Function*>> Fs_map;
    Fs_map[0] = {f0};
    Fs_map[1] = {f1};
    Fs_map[2] = {f1_0};
    Fs_map[3] = {f1_1};
    Fs_map[4] = {f1_2};
    Fs_map[5] = {f2};
    Fs_map[6] = {f3};
    Fs_map[7] = {f4, f6};
    Fs_map[8] = {f5};
    // Fs_map[9] = {f6};
    Fs_map[9] = {f7};
    Fs_map[10] = {f8};
    // Fs_map[12] = {f1_2};
    // Fs_map[13] = {f1_3};
    // Fs_map[14] = {f1_4};
    // Fs_map[15] = {f2_0};
    // Fs_map[12] = {f2_1};
    // Fs_map[13] = {f2_2};
    // Fs_map[16] = {f3_0};
    // Fs_map[15] = {f3_1};
    // Fs_map[16] = {f3_2};
    // Fs_map[17] = {f7_0};
    // Fs_map[18] = {f7_1};
    // Fs_map[19] = {f7_2};
    // Fs_map[20] = {f8_0};
    // Fs_map[21] = {f8_1};
    // Fs_map[22] = {f8_2};
    // Fs_map[23] = {f6_0};
    // 构造app
    App app(0, Fs_map, &smm, &gpu_smm);
    // 运行app
    app.check();
    app.printMsg();
    app.init();
    app.run();
    app.waitForComplete();
}

int main(){
    // 注册
    registe();
    // 声明共享内存管理对象
    SharedMemoryManager smm;
    Gpu_SharedMemoryManager gpu_smm;
    // 将视频加载到内存中
    SharedMemoryPool input_video_pool = smm.createSharedMemoryPool("input_video", Cv_Mat_Data_3840_2160_Memory, cnt);
    SharedMemoryPool output_video_pool = smm.createSharedMemoryPool("output_video", Cv_Mat_Data_3840_2160_Memory, cnt);
    input_video_data_ptr = input_video_pool.getDataPtr();
    output_video_data_ptr = output_video_pool.getDataPtr();
    Image::Gen::genFromDisk f_start(video_path, input_video_data_ptr, cnt, 30, 3840, 2160);
    f_start.run();
    // 进行测试
    test(smm, gpu_smm);
    // 检查是否存在内存泄露
    smm.check();
    // 将输出视频保存到磁盘
    // Image::Save::save f_end(save_path, output_video_data_ptr, cnt, 30, 3840, 2160);
    // f_end.run();
}